package com.xingchi.tornado.storage.core.controller;

import com.xingchi.tornado.basic.Result;
import com.xingchi.tornado.storage.core.model.FileUploadWrapper;
import com.xingchi.tornado.storage.core.model.dto.FileUserRelDTO;
import com.xingchi.tornado.storage.core.model.vo.FileVO;
import com.xingchi.tornado.storage.core.service.FileService;
import com.xingchi.tornado.storage.core.service.FileUploadService;
import com.xingchi.tornado.utils.BeanUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 文件服务控制器
 *
 * @author xingchi
 * @date 2024/9/1 15:35
 */
@Slf4j
@RestController
@Tag(name = "文件操作控制器", description = "包含文件查询、校验等相关操作")
@RequestMapping("/file")
public class FileController {

    @Resource
    private FileService fileService;
    @Resource
    private FileUploadService fileUploadService;

    @Operation(summary = "根据文件的sha256值查询文件是否存在", description = "根据文件的sha256值查询文件是否存在，大文件上传时提前判断文件是否存在")
    @GetMapping("/check/{sha256}")
    public Result<Boolean> exists(@PathVariable("sha256") String sha256) {
        return Result.ok(fileService.exists(sha256));
    }

    @Operation(summary = "根据文件id，获取文件详情", description = "根据文件id获取文件详情信息")
    @GetMapping("/{id}")
    public Result<FileVO> get(@PathVariable("id") Long id) {
        return Result.ok(BeanUtils.convert(fileService.selectById(id), FileVO.class));
    }

    @Operation(summary = "上传文件", description = "上传文件")
    @PostMapping("/upload")
    public Result<FileUserRelDTO> uploadFile(@RequestParam("file") MultipartFile file) {
        FileUploadWrapper holder = FileUploadWrapper.wrap(file);
        return Result.ok(fileUploadService.uploadFile(holder));
    }

    @Operation(summary = "获取文件访问URL", description = "获取文件访问URL")
    @GetMapping("/getUrl")
    public Result<String> getUrl(@RequestParam("path") String path) {
        return Result.ok(fileUploadService.getUrl(path));
    }

    @Operation(summary = "获取文件访问URL", description = "获取文件访问URL")
    @GetMapping("/getUrlById")
    public Result<String> getUrlById(@RequestParam("id") Long id) {
        return Result.ok(fileUploadService.getUrl(id));
    }

    @Operation(summary = "根据文件存储路径下载文件", description = "根据文件存储路径下载文件")
    @GetMapping("/download")
    public void download(@RequestParam("path") String path) {
        fileUploadService.download(path);
    }

    @Operation(summary = "根据文件id下载文件", description = "根据文件id下载文件")
    @GetMapping("/download/{id}")
    public void download(@PathVariable Long id) {
        fileUploadService.download(id);
    }
}
